package com.jservice.api.service.upp;

import com.alibaba.fastjson.JSONObject;
import com.jservice.api.MsgCode;
import com.jservice.api.UppClient;
import com.jservice.api.dto.Request;
import com.jservice.api.dto.Response;
import com.jservice.api.dto.ResponseHeader;
import com.jservice.api.service.BaseService;
import com.jservice.api.service.IService;
import com.jservice.api.smkAppUser.AppUserKit;
import com.jservice.framework.dto.Setting;
import com.jservice.framework.idWorker.Sequence;
import com.jservice.model.SmkAppUser;
import com.jservice.model.SmkRefunds;
import com.jservice.web.smkSn.SnService;
import com.wondersgroup.upp.api.core.UPPlatformRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Upp2021 - 充值退款
 *
 * @Author cwledit
 * @DATE 2017/6/14.
 */
@Service("upp2021")
@Transactional
public class Upp2021 extends BaseService implements IService {

    private String SERVER_NAME = "upp.trade.refund";

    @Value("")
    private String host;

    @Resource
    private SnService snService;

    @Resource
    private Sequence sequence;

    @Override
    public Response excute(Request request, Response response, ResponseHeader header, String method) throws IOException {
        Map paramMap = request.getData();
        String card_no = paramsStringFilter(paramMap.get("card_no"));
        String trade_no = paramsStringFilter(paramMap.get("trade_no"));
        String refund_amount = paramsStringFilter(paramMap.get("refund_amount"));
        String trans_code = paramsStringFilter(paramMap.get("trans_code"));
        String out_refund_no = paramsStringFilter(paramMap.get("out_refund_no")); //新添加

        SmkAppUser smkAppUser = AppUserKit.getCurrent(request);
        if (smkAppUser == null) {
            return renderFAIL(MsgCode.Code_1007, response, header);
        }
        Map data = new HashMap();
        data.put("card_no", card_no);
        data.put("id_no", smkAppUser.getIdCard());
        data.put("id_type", "01");
        data.put("trade_no", trade_no);
        data.put("refund_amount", refund_amount);
        data.put("trans_code", trans_code);

//        String sn = snService.generate(SnService.Type.refunds);
//        SmkRefunds smkRefundsTmp = SmkRefunds.dao.doFindFirst("trade_no = ? ",trade_no);
//        if (smkRefundsTmp != null){
//            sn = smkRefundsTmp.getSn();
//        }

//        data.put("out_refund_no", sn);
        data.put("out_refund_no", out_refund_no); //新添加
        data.put("notify_url", Setting.getConfig("smkRefund_notify_url"));
        data.put("auth_type", "032");


        UPPlatformRequest uRequest = getUPRequest(SERVER_NAME, request.getHeader());
        uRequest.setBiz_content(sortParam(data));
        UppClient client = UppClient.getInstance();
        JSONObject jsonObject = client.doRequest(uRequest);
        if ("000000".equals(jsonObject.getString("code"))) {
            jsonObject.remove("code");
            //退款单
            SmkRefunds smkRefunds = new SmkRefunds();
            smkRefunds.setId(sequence.nextIdStr());
            smkRefunds.setStatus("3");//3:处理中
            smkRefunds.setCreateDate(new Date());
            smkRefunds.setModifyDate(new Date());
            smkRefunds.setAmount(refund_amount);
            smkRefunds.setRefundTradeNo(jsonObject.getString("refund_trade_no"));
            smkRefunds.setTradeNo(trade_no);
            smkRefunds.setSn(out_refund_no);//新添加
//            smkRefunds.setSn(sn);
            smkRefunds.save();
            return renderSUC(jsonObject, response, header);
        } else {
            return renderFAIL(MsgCode.Code_1009, "市民卡网关返回异常:" + jsonObject.getString("msg"), response, header);
        }
    }
}
